import request from '/utils/request'

export const mallApi = {
  /** 获取商品分类 */
  getCategories(): Promise<DomoFeedCategory[]> {
    return request.get(`/mall/categories`)
  },
  /** 获取商品分类列表 */
  getCommodities(params?: GetCommoditiesParmas) {
    return request.get(`/mall/commodities`, { params })
  },
  /** 获取商品详情 */
  getCommodityDetail(commodityId: number, params = {}): Promise<DomoCommodity> {
    return request.get(`/mall/commodities/${commodityId}`, { params })
  },

  /** 获取商品的可用分享次数 */
  getCommodityShareCount(commodityId: number): Promise<CommodityShareCallback> {
    return request.get(`/mall/discount-shares/${commodityId}`)
  },

  /** 通知服务器分享成功 */
  putCommodityShare(commodityId: number): Promise<CommodityShareCallback> {
    return request.put(`/mall/discount-shares/${commodityId}`)
  },

  /** 创建商品订单 */
  postMallOrder(payload: any) {
    payload.pay_method = 'WechatPay_Js'
    // payload.app_type = 'micro'
    return request.post('/mall/orders', payload)
  },
  /** 商品分享 */
  putShareProduct() {
    const sum = 3
    const commodityId = 233
    return request.put(`/mall/commodities/${commodityId}/hit-share`, { incr: sum })
  },
  /** 获取商品的评论列表 */
  async getCommodityCommodities(
    commodityId: number,
    params: GetCommoditiesCommoditiesParams = {},
  ): Promise<DomoFeed[]> {
    return request.get(`/mall/commodities/${commodityId}/feeds`, { params })
  },

  /** 收藏商品 */
  async collectCommodities(commodityId: number): Promise<void> {
    return request.put(`/mall/commodities/${commodityId}/like`)
  },

  /** 取消收藏商品 */
  async unCollectCommodities(commodityId: number): Promise<void> {
    return request.delete(`/mall/commodities/${commodityId}/like`)
  },
  /** 获取单个订单 */
  getMallOrder(orderId: number, params: any) {
    return request.get(`/mall/orders/${orderId}`, { params })
  },
  /** 获取订单列表 */
  getMallOrders(params: GetOrderListParmas) {
    return request.get('/mall/orders', { params })
  },
  /** 修改单个订单 */
  resetMallOrder(orderId: number, params: DeletOrderPramas) {
    return request.patch(`/mall/orders/${orderId}`, params)
  },
  /** 获取订单快递列表 */
  getExpressesList(orderId: number): Promise<ExpressesCallback[]> {
    return request.get(`/mall/orders/${orderId}/expresses`)
  },
  /** 获取物流详情 */
  getExpresses(expressesId: string): Promise<ExpressesCallback> {
    return request.get(`/mall/expresses/${expressesId}`)
  },

  /** 获取快递公司列表 */
  getCompanies(params?: GetCompaniesParams): Promise<PageCompaniesReturn> {
    return request.get('/kd100/companies', { params })
  },

  /** 添加订单快递 */
  addOrderExpresses(payload: any) {
    return request.post('/mall/expresses', payload)
  },

  /** 删除单个快递 */
  deletExpresses(expressId: number) {
    return request.delete(`/mall/expresses/${expressId}`)
  },

  /** 修改单个快递 */
  resetExpresses(expressId: number, payload: ResetExpressesParam) {
    return request.patch(`/mall/expresses/${expressId}`, payload)
  },
  /**获取商品弹幕 */
  getProductBarrage(commodityId: number) {
    return request.get(`/mall/commodities/${commodityId}/orders-subtitles`)
  },
  /** 检测用户是否可以发布商品 */
  canPublishProduct(params: any): Promise<{ can_publish: boolean }> {
    return request.get(`/mall/commodity/can-publish`, { params })
  },
  /** 添加商品 */
  creatProduct(payload: any) {
    return request.post(`/mall/commodities`, payload)
  },

  /** 修改商品 */
  editProduct(payload: any, id: any) {
    return request.put(`/mall/commodities/${id}`, payload)
  },
  /** 删除商品 */
  deleteProduct(commodityId: number) {
    return request.delete(`/mall/commodities/${commodityId}`)
  },
  /** 上架或者下架商品 */
  rackUpOrDownProduct(commodityId: number, deleted: boolean) {
    return request.patch(`/mall/commodities/${commodityId}`, { deleted })
  },
  /** 修改商品信息 */
  editorProduct(commodityId: number, payload: any) {
    return request.put(`/mall/commodities/${commodityId}`, payload)
  },
  /** 获取品牌列表 */
  getBrands(params: any): Promise<any[]> {
    return request.get('/mall-brands', { params })
  },
  /** 确认收货 */
  confirmOrder(orderId: any) {
    return request.post(`/mall/orders/${orderId}/confirm`)
  },
  /** 加入购物车 */
  joinCarts(payload: any) {
    return request.post('/mall/shopping-carts', payload)
  },
  /** 获取购物车列表 */
  getCartsList(params: any) {
    return request.get('/mall/shopping-shopkeeper-carts', { params })
  },
  /** 修改购物车商品 */
  changeCarts(id: number, payload: any) {
    return request.patch(`/mall/shopping-carts/${id}`, payload)
  },
  /** 批量删除购物车商品 */
  deletCarts(payload: any) {
    return request.post('/mall/shopping-carts/multiple-destroy', payload)
  },
  /** 购物车结算 */
  payCartsOrder(payload: any) {
    return request.post('/mall/multipleOrders', payload)
  },
  /** 购物车商品数量 */
  getCartsCount() {
    return request.get('/mall/shopping-carts-count')
  },
  /** 修改商家二维码和微信 */
  saveShoperInfo(payload: ShoperInfoParam) {
    return request.post('/mall/shop-info', payload)
  },
  /** 获取商家联系信息(二维码) */
  getShoperInfo() {
    return request.get('/mall/shop-info')
  },
  /** 卖家信息检测 */
  checkShoperInfo() {
    return request.get('/mall/shop-info-check')
  },
  /** 删除知识订单 */
  resetKnowledgeOrder(orderId: number, params: DeletOrderPramas) {
    return request.patch(`/knowledge/orders/${orderId}`, params)
  },
  /** 取消订单 */
  cancelOrder: (orderId: string | number) => request.put(`/mall/orders/${orderId}/cancel`),
  /** 重新支付 */
  payAgin: (orderId: string | number, payload: any) =>
    request.put(`/mall/orders/${orderId}`, payload),
  /** 修改订单信息 */
  editOrder: (orderId: string | number, payload: any) =>
    request.patch(`/mall/orders/${orderId}`, payload),

  // 买家申请退款/退货退款
  postRefund: (orderId: string | number, payload: PostRefundParams): Promise<DomoRefundIntercept> =>
    request.post(`/mall/orders/${orderId}/refund`, payload),
  // 买家取消退款.（申请退款中/申请退货中/卖家同意退货）状态时可申请
  cancelRefund: (orderId: string | number) => request.delete(`/mall/refunds/${orderId}`),
  // 买家填写退货物流（买家退货）
  postExpresses: (payload: PostExpressesParams): Promise<DomoRefundIntercept> =>
    request.post(`/mall/refund/expresses`, payload),
  // 买家编辑退款/退货退款
  putExpresses: (id: string | number, payload: PostExpressesParams): Promise<DomoRefundIntercept> =>
    request.put(`/mall/refund/expresses/${id}`, payload),

  // 卖家同意退款（未发货）
  agreeRefund: (orderId: string | number) => request.put(`/mall/refunds/${orderId}/refund-agree`),
  // 卖家驳回退款（未发货）
  rejectRefund: (retId: string | number, payload: RefundParams) =>
    request.put(`/mall/refunds/${retId}/refund-refuse`, { refusal_reason: payload }),
  // 卖家拦截发货并退款（已发货）
  interceptRefund: (orderId: string | number): Promise<DomoRefundIntercept> =>
    request.put(`/mall/refunds/${orderId}/refund-intercept`),
  // 卖家同意退货退款（已发货）
  agreeReturn: (retId: string | number, payload: AgreeReturnParams): Promise<DomoRefundIntercept> =>
    request.post(`/mall/refunds/${retId}/return-agree`, { address: payload }),
  // 卖家驳回退货退款（已发货）
  rejectReturn: (retId: string | number, payload: RefundParams) =>
    request.put(`/mall/refunds/${retId}/return-refuse`, { refusal_reason: payload }),
  // 卖家确认收货并退款
  confirmRefund: (orderId: string | number) =>
    request.put(`/mall/refunds/${orderId}/confirm-refund`),
  // 卖家确认收货需协商价格
  negotiateRefund: (retId: string | number) =>
    request.put(`/mall/refunds/${retId}/confirm-negotiate`),

  // 买家申请协商退款
  postRefundNegotiate: (orderId: string | number, payload: RefundNegotiateParams) =>
    request.post(`/mall/refunds/${orderId}/negotiate`, payload),
  // 卖家同意协商退款申请
  agreeRefundNegotiate: (id: string | number) => request.put(`/mall/negotiates/${id}/agree`),
  // 卖家驳回协商退款申请
  refuseRefundNegotiate: (id: string | number, payload: RefundParams) =>
    request.put(`/mall/negotiates/${id}/refuse`, { refusal_reason: payload }),
  // 协商成功，退款失败后，卖家操作退款
  againRefundNegotiate: (id: string | number, payload: RefundParams) =>
    request.put(`/mall/negotiates/${id}/refund-again`, { refusal_reason: payload }),

  // 订单申诉
  orderAppeal: (orderId: string | number, payload: RefundParams) =>
    request.post(`/mall/orders/${orderId}/appeal`, payload),
  // 订单反馈
  orderFeedback: (orderId: string | number, payload: RefundParams) =>
    request.post(`/mall/orders/${orderId}/feedback`, payload),
  // 获取商品配置信息
  getMallConfig: () => request.put(`/mall/configs`),
}
